//
//  AppDelegate.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/5.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "MessageModel.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCellularData.h>
#import "MeVipViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self requestNetworkPermission];
    if (![[JQFMDB shareDatabase]jq_isExistTable:HomeHistoryTable]) {
        [[JQFMDB shareDatabase]jq_createTable:HomeHistoryTable dicOrModel:[MessageModel class]];
    }
    
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Manager SQ_getViewController]];
    self.window  = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:nav];
    self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)requestNetworkPermission {
#if TARGET_IPHONE_SIMULATOR
    // 在模拟器上运行的代码
    NSLog(@"在模拟器上运行");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [HttpTool SQ_get:@"https://www.jindong.com" ParamDic:[NSMutableDictionary dictionary] successBlock:^(BaseModel * _Nonnull httpModel) {
            
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            
        }];
    });
#else
    // 在真机上运行的代码
    NSLog(@"在真机上运行");
    // 判断是否已经获得网络权限
    if (@available(iOS 9.0, *)) {
        CTCellularData *cellularData = [[CTCellularData alloc] init];
        cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
            if (state == kCTCellularDataRestricted) {
                // 网络权限被限制，可以在这里进行相应处理
                NSLog(@"网络权限被限制");
                [HttpTool SQ_get:@"https://www.baidu.com" ParamDic:[NSMutableDictionary dictionary] successBlock:^(BaseModel * _Nonnull httpModel) {
                    
                } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
                    
                }];
            } else if (state == kCTCellularDataNotRestricted) {
                // 网络权限可用，可以在这里进行相应处理
                NSLog(@"网络权限可用");
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [self registerHX];
                });
            }else{
                NSLog(@"%ld",state);
            }
        };
    } else {
        // iOS 9以下的版本，可以在这里进行相应处理
        NSLog(@"iOS版本低于9.0");
    }
#endif
}

#pragma mark - 程序将要进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  
    if ([[UserModel share].token isNotEmpty]) {
        UIViewController * vc = [Manager SQ_getCurrentViewController];
        if ([vc isKindOfClass:[MeVipViewController class]]) {
            DTLog(@"---222---程序将要进入前台");
        } else {
            if (![UserModel share].isVip) {
                MeVipViewController* vcs= [[MeVipViewController alloc]init];
                [vc.navigationController pushViewController:vcs animated:NO];
            }
        }
    }
    DTLog(@"---333---程序将要进入前台");
}
 
@end
